#include "StdAfx.h"
#include "Geometry.h"
#include "Utility\ColladaGeometry.h"
#include "DxIndexBuffer.h"
#include "DxVertexBuffer.h"
#include "DxTexture.h"


Geometry::Geometry(ColladaGeometry* geometry)
{
	m_pVertices = new DxVertexBuffer();
	m_pVertices->CreateBuffer(geometry->GetVertexCount());
	m_pVertices->Lock();
	for(int i = 0; i < geometry->GetVertexCount(); i++)
	{
		m_pVertices->SetPosition(i, geometry->GetPosition(i));
		m_pVertices->SetNormal(i, geometry->GetNormal(i));
		m_pVertices->SetTexCoord(i, geometry->GetUV(i));
		m_pVertices->SetBlendWeights(i, geometry->GetBlendWeights(i));
		m_pVertices->SetBlendIndices(i, geometry->GetBlendIndices(i));
	}
	m_pVertices->UnLock();

	m_pIndices = new DxIndexBuffer();
	m_pIndices->CreateIndexBuffer(geometry->GetIndexCount());
	m_pIndices->Lock();
	m_pIndices->SetIndices(&geometry->GetIndices());	
	m_pIndices->UnLock();

	char* textureFile = geometry->GetTextureFile();

	if( textureFile )	
		SetTexture(textureFile);
	else
		m_pTexture = 0;	
}


Geometry::~Geometry(void)
{
	if( m_pVertices )
		delete m_pVertices;
	if( m_pIndices )
		delete m_pIndices;
	if( m_pTexture )
		delete m_pTexture;
}

void Geometry::SetTexture(const char* pszText)
{
	m_pTexture = new DxTexture();
	m_pTexture->Load(pszText);
}